package com.gem.entity;

import com.gem.enums.GemType;
import com.gem.factory.game.GameFactory;
import lombok.Data;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Author: Xhy
 * CreateTime: 2022-09-24 16:46
 * 贵族
 */
@Data
public class Nobility {

    // 规则需要的换取卡牌信息
    private  Map<Integer,Integer> cards;

    // 得分
    private Integer score;

    // 贵族s
    private static List<Nobility> nobilityS = new ArrayList<>();

    private static Map<Integer,Nobility> factory = new HashMap<>();

    public static Integer tempNo = 1;

    private Integer no;

    static {
        add(new Nobility().blue(4).green(4));
        add(new Nobility().green(3).blue(3).white(3));
        add(new Nobility().green(4).black(4));
        add(new Nobility().black(4).white(4));
        add(new Nobility().black(4).red(4));
        add(new Nobility().green(3).black(3).red(3));
        add(new Nobility().blue(4).white(4));
        add(new Nobility().red(4).green(4));
        add(new Nobility().black(3).white(3).green(3));
        add(new Nobility().blue(3).white(3).red(3));
        add(new Nobility().red(4).white(4));
        add(new Nobility().black(3).red(3).blue(3));
        add(new Nobility().black(3).red(3).white(3));
        add(new Nobility().black(3).red(3).green(3));
        add(new Nobility().black(3).blue(3).white(3));
    }

    private static void add(Nobility nobility){
        nobilityS.add(nobility);
        factory.put(nobility.no,nobility);
    }

    public static List<Nobility> init(int number){
        HashSet<Nobility> set = new HashSet<>();
        int s = nobilityS.size();
        while (set.size()!=number+1){
            int random = new Random().nextInt(s);
            set.add(nobilityS.get(random));
        }
        return set.stream().collect(Collectors.toList());
    }

    public Nobility(){
        cards = new HashMap<>();
        this.no = tempNo++;
        score(3);
    }
    public void score(int score){
        this.score = score;
    }
    public Nobility green(int number){
        this.cards.put(GemType.GREEN.type,number);
        return this;
    }
    public Nobility blue(int number){
        this.cards.put(GemType.BLUE.type,number);
        return this;
    }
    public Nobility white(int number){
        this.cards.put(GemType.WHITE.type,number);
        return this;
    }
    public Nobility black(int number){
        this.cards.put(GemType.BLACK.type,number);
        return this;
    }
    public Nobility red(int number){
        this.cards.put(GemType.RED.type,number);
        return this;
    }


    public static boolean exist(String roomId,Integer no){
        Game game = GameFactory.get(roomId);
        for (Nobility nobility : game.getNobilities()) {
            if (no == nobility.no){
                return true;
            }
        }
        return false;
    }
    public static Nobility get(Integer no){
        return factory.get(no);
    }
}
